/***
 * * PROJECT
 * *    Logical Neighborhoods - J2ME Run-Time Support 
 * * VERSION
 * *    $LastChangedRevision: 118 $
 * * DATE
 * *    $LastChangedDate: 2008-05-28 19:35:12 +0200 (Wed, 28 May 2008) $
 * * LAST_CHANGE_BY
 * *    $LastChangedBy: lmottola $
 * *
 * *    $Id: AtomicPredicate.java 118 2008-05-28 17:35:12Z lmottola $
 * *
 * *   Logical Neighborhoods - 
 * *                   Programming Wireless Sensor Networks
 * *                   (One Slice at a Time)
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU Lesser General Public License
 * *   as published by the Free Software Foundation; either version 2.1
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU Lesser General Public 
 * *   License along with this program; if not, you may find a copy at 
 * *   the FSF web site at 'www.gnu.org' or 'www.fsf.org', or you may 
 * *   write to the Free Software Foundation, Inc., 59 Temple 
 * *   Place - Suite 330, Boston, MA  02111-1307, USA
 ***/

package atag.runtime.ln.neighborhoodDefs;

import java.io.*;

import atag.runtime.ln.nodeAttributes.NodeAttribute;

/**
 * Atomic predicates involved in the definition of neighborhoods must implement
 * this interface by providing the required matching semantics.
 * 
 * @author Luca Mottola <a
 *         href="mailto:mottola@elet.polimi.it">mottola@elet.polimi.it</a>
 * 
 */
public abstract class Predicate {
	
	public final static int INTEGER_SIMPLE = 0;
	public final static int STRING_SIMPLE = 1;
	public final static int INTEGER_RANGE = 2;
	public final static int STRING_SET = 3;
	public final static int BOOLEAN_SIMPLE = 4;
	public final static int DOUBLE_SIMPLE = 5;

	/**
	 * Must implement the matching semantics for a node attribute, specifying
	 * the conditions under which this predicate matches.
	 * 
	 * @param attr
	 *            the node attribute to match against.
	 * @return true if the predicate matches the attribute, false otherwise.
	 */
	public abstract boolean matches(NodeAttribute attr);

	public abstract DataOutput serialize(DataOutput dg) throws IOException;
	
	public static Predicate deserialize(DataInput dg) throws IOException {
		
		byte type = dg.readByte();
		
		switch (type) {
		
		case INTEGER_SIMPLE:
			return IntegerSimplePredicate.deserialize(dg);
			
		case STRING_SIMPLE:
			return StringSimplePredicate.deserialize(dg);
			
		case INTEGER_RANGE:
			return IntegerRangePredicate.deserialize(dg);
			
		case STRING_SET:
			return StringSetMembershipPredicate.deserialize(dg);
			
		case BOOLEAN_SIMPLE:
			return BooleanSimplePredicate.deserialize(dg);
			
		case DOUBLE_SIMPLE:
			return DoubleSimplePredicate.deserialize(dg);
			
		default:
			return null;
		}
	}

}
